<?php
namespace app\service;

use support\facade\Logger;
use const support\refund_rule_1;
use const support\refund_rule_2;
use const support\refund_rule_3;
use const support\refund_rule_4;
use const support\refund_rule_remarks;

class RefundRulesService{

    protected static $refundRuleType=0;

    protected static $refundRuleTitle='';

    protected static $actualRefundAmount=0;

    protected static $originRefundAmount=0;

    //第一版退费规则
    public static function refundAlgorithmV1(float $refundAmount,int $goodsPeriodDate,int $isResisted=0): array
    {
//        退费原则
//        1、因不可抗原因取消活动，全额退款。
//        2、因个人原因无法参加：
//        1）在活动开始前72小时前申请即全额退款。
//        2）在活动开始前48小时前申请退还50%。
//        3）在活动开始24小时前申请无法退款，敬请谅解。
        if ($isResisted){
            self::$refundRuleType=refund_rule_1;
            self::$refundRuleTitle=refund_rule_remarks[refund_rule_1];
            self::$actualRefundAmount=$refundAmount;
        }else{
            //获取当前时间
            $surplusTime=$goodsPeriodDate-time();
            Logger::app('申请退款操作:退费规则','info',[$surplusTime,time(),$surplusTime]);
            if ($surplusTime<=0){
                //当天无法退款
                self::$refundRuleType=refund_rule_4;
                self::$refundRuleTitle=refund_rule_remarks[refund_rule_4];
                self::$actualRefundAmount=0;
            }elseif ($surplusTime>=72*3600){
                //提前72小时全部退款
                self::$refundRuleType=refund_rule_2;
                self::$refundRuleTitle=refund_rule_remarks[refund_rule_2];
                self::$actualRefundAmount=$refundAmount;
            }elseif ($surplusTime>=48*3600){
                self::$refundRuleType=refund_rule_3;
                self::$refundRuleTitle=refund_rule_remarks[refund_rule_3];
                self::$actualRefundAmount=$refundAmount*0.5;
            }else{
                self::$refundRuleType=refund_rule_4;
                self::$refundRuleTitle=refund_rule_remarks[refund_rule_4];
                self::$actualRefundAmount=0;
            }
        }
        self::$originRefundAmount=$refundAmount;
        return self::returnData();
    }

    //统一返回数据
    protected static function returnData(): array
    {
        return [
            'refund_rule_type'      =>self::$refundRuleType,
            'refund_rule_title'     =>self::$refundRuleTitle,
            'actual_refund_amount'  =>self::$actualRefundAmount,
            'origin_refund_amount'  =>self::$originRefundAmount
        ];
    }

}